package com.sun.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangquanwei
 * @version 1.0
 * @since 2025-03-07 14:17
 */
public class MyBatisPlusGenerator {
    // 数据库连接配置
    public static final String MODEL = "/sunpay-dao";
    public static final String THIS_MODULE_NAME = "sunpay-dao"; //当前项目名称
    public static final String DB_URL = "jdbc:mysql://rm-uf60g0dop706q6uihmo.mysql.rds.aliyuncs.com:3306/sunpay?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
    public static final String DB_USERNAME = "sun";
    public static final String DB_PASSWORD = "Zqw123456";
    public static final String AUTHOR = "zhangquanwei";

    // 多个用,拼接
    public static final String TABLE_NAMES = "t_sys_role";

    public static void main(String[] args) {
        // 模块路径
        String daoModulePath = System.getProperty("user.dir") + "/sunpay-dao";
        String serviceModulePath = System.getProperty("user.dir") + "/sunpay-service";

        // 代码生成器配置
        FastAutoGenerator.create(DB_URL, DB_USERNAME, DB_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
                            .enableSwagger() // 开启 Swagger 注解
                            .disableOpenDir(); // 禁用打开生成目录
                })
                .packageConfig(builder -> {
                    // 设置父包名
                    builder.parent("com.sun")
                            .moduleName("") // 模块名（可选）
                            .entity("entity") // Entity 包名
                            .mapper("mapper") // Mapper 包名
                            .service("service"); // Service 包名
                            //.controller("controller"); // Controller 包名

                    // 设置不同文件的输出路径
                    Map<OutputFile, String> pathInfo = new HashMap<>();
                    pathInfo.put(OutputFile.entity, daoModulePath + "/src/main/java/com/sun/entity"); // Entity 输出路径
                    pathInfo.put(OutputFile.mapper, daoModulePath + "/src/main/java/com/sun/mapper"); // Mapper 输出路径
                    pathInfo.put(OutputFile.xml, daoModulePath + "/src/main/resources/mapper"); // XML 输出路径
                    pathInfo.put(OutputFile.service, serviceModulePath + "/src/main/java/com/sun/service"); // Service 输出路径
                    //pathInfo.put(OutputFile.controller, serviceModulePath + "/src/main/java"); // Controller 输出路径

                    builder.pathInfo(pathInfo);
                })
                .strategyConfig(builder -> {
                    builder.addInclude(TABLE_NAMES) // 需要生成的表名
                            .addTablePrefix("t_", "sys_") // 表前缀过滤
                            .entityBuilder() // Entity 配置
                            .enableLombok() // 启用 Lombok
                            .columnNaming(NamingStrategy.underline_to_camel) // 字段映射规则：下划线转驼峰
                            .idType(IdType.AUTO) // 主键策略
                            .mapperBuilder() // Mapper 配置
                            .enableMapperAnnotation() // 启用 @Mapper 注解
                            .serviceBuilder() // Service 配置
                            .formatServiceFileName("%sService") // Service 文件命名格式
                            .controllerBuilder() // Controller 配置
                            .enableRestStyle() // 启用 RestController
                            .enableFileOverride(); // 覆盖已生成文件
                })
                .templateEngine(new VelocityTemplateEngine()) // 使用 Velocity 模板引擎
                .execute();
    }
}
